package com.bms.cfcmms.util;

import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ResourceBundle;

/**
 * Created by carl on 2016/6/1.
 */
public class SysUtils {
    private static Logger log = LoggerFactory.getLogger(SysUtils.class);
    private static String INSTALL_PATH = "";

    private SysUtils() {
        throw new IllegalAccessError("SysUtils class");
    }
    /**
     * 获取tomcat的安装路径.
     *
     * @return  tomcat 安装路径
     */
    public static String getInstallPath() {
        if (StringUtils.hasLength(INSTALL_PATH)) {
            return INSTALL_PATH;
        } else {
            String rootPath = SysUtils.class.getClassLoader().getResource("").getPath();
            int index = rootPath.indexOf("WEB-INF");
            if (index == -1) {
                INSTALL_PATH = rootPath;
            } else {
                INSTALL_PATH = rootPath.substring(0, index) + File.separator;
            }
            log.info("获取到系统的安装路径为：" + INSTALL_PATH);
        }
        return INSTALL_PATH;
    }

    public static Object getClassObj(Class refClass) {
        Class<T> operObj = null;
        Type type = refClass.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType) type).getActualTypeArguments();
            operObj = (Class<T>) types[0];
        }
        return operObj;
    }

    public static String findConstantValue(String key) {
        ResourceBundle resource = ResourceBundle.getBundle("constant");
        return resource.getString(key);
    }

    /**
     * 将Long类型的IP转换为字符串格式的IP地址.
     *
     * @param ipLong Long类型的IP地址
     * @return 字符串类型的IP地址
     */
    public static String getLongIpToString(Long ipLong) {
        if (ipLong == null || ipLong == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(ipLong >>> 24);
        sb.append(".");
        sb.append(String.valueOf((ipLong & 0x00FFFFFF) >>> 16));
        sb.append(".");
        sb.append(String.valueOf((ipLong & 0x0000FFFF) >>> 8));
        sb.append(".");
        sb.append(String.valueOf(ipLong & 0x000000FF));
        return sb.toString();
    }

    /**
     * 将字符串格式的IP转换为Long类型的IP地址.
     *
     * @param ipString 字符串类型的IP地址
     * @return Long类型的IP地址
     */
    public static Long getStringIpToLong(String ipString) {
        if (ipString == null || "".equals(ipString)) {
            return 0L;
        }
        long result = 0;
        java.util.StringTokenizer token = new java.util.StringTokenizer(ipString, ".");
        result += Long.parseLong(token.nextToken()) << 24;
        result += Long.parseLong(token.nextToken()) << 16;
        result += Long.parseLong(token.nextToken()) << 8;
        result += Long.parseLong(token.nextToken());
        return result;
    }
}
